package com.lineage.data.npc.other;

import com.lineage.DatabaseFactory;
import com.lineage.config.ConfigOther;
import com.lineage.server.IdFactory;
import com.lineage.server.IdFactoryNpc;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Inventory;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_BlueMessage;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.RandomArrayList;
import com.lineage.server.utils.SQLUtil;
import com.lineage.server.world.World;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

public class exp_game07 {
  private static final int _bossId1 = ConfigOther.Textnpa1;
  
  private static final int _bossId2 = ConfigOther.Textnpa2;
  
  private static final int _bossId3 = ConfigOther.Textnpa3;
  
  private static final int _bossId4 = ConfigOther.Textnpa4;
  
  private static final int _bossId5 = ConfigOther.Textnpa5;
  
  private static final int _bossId6 = ConfigOther.Textnpa6;
  
  private static final int _bossId7 = ConfigOther.Textnpa7;
  
  private static final int _bossId8 = ConfigOther.Textnpa8;
  
  private static final int _bossId9 = ConfigOther.Textnpa9;
  
  private static final int _bossId10 = ConfigOther.Textnpa10;
  
  private static final int _adena = ConfigOther.TextMoney;
  
  private final ArrayList<L1PcInstance> _members = new ArrayList<>();
  
  protected L1NpcInstance _targetNpc = null;
  
  private int _TextTaskStatus = 0;
  
  private int _DailyGameTime07 = 0;
  
  private static exp_game07 _instance;
  
  public static final int STATUS_NONE = 0;
  
  public static final int STATUS_READY = 1;
  
  public static final int STATUS_PLAYING = 2;
  
  public static final int STATUS_CLEANING = 4;
  
  public static exp_game07 getInstance() {
    if (_instance == null)
      _instance = new exp_game07(); 
    return _instance;
  }
  
  public String enterTextTask(L1PcInstance pc) {
    if (pc.getLevel() < 95 && !isMember(pc)) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("请依照自身等级进入经验副本"));
      return "";
    } 
    if (pc.getQuest().get_step(8592) >= 3) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("今日已达上限次数"));
      return "";
    } 
    if (getInstance().getTextTaskStatus() == 4) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("此经验副本清洁时间1分钟 请稍候"));
      return "";
    } 
    if (getInstance().getTextTaskStatus() == 2) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("此经验副本已启动.剩余时间" + getDailyGameTime07() + "秒"));
      return "";
    } 
    if (!pc.getInventory().checkItem(40308, 500000L)) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(189));
      return "";
    } 
    if (pc.getQuest().get_step(8592) == 0) {
      pc.getQuest().set_step(8592, 1);
    } else if (pc.getQuest().get_step(8592) == 1) {
      pc.getQuest().set_step(8592, 2);
    } else if (pc.getQuest().get_step(8592) == 2) {
      pc.getQuest().set_step(8592, 3);
    } 
    L1Teleport.teleport(pc, 32774, 32790, (short)8523, pc.getHeading(), true);
    addMember(pc);
    return "";
  }
  
  private void addMember(L1PcInstance pc) {
    if (!this._members.contains(pc)) {
      this._members.add(pc);
      pc.getInventory().consumeItem(40308, 500000L);
    } 
    if (getMembersCount() == 1 && getTextTaskStatus() == 0)
      GeneralThreadPool.get().execute(new runTextTask()); 
  }
  
  private void endTextTask() {
    setTextTaskStatus(4);
    sendMessage("经验副本时间到达.已关闭");
    L1PcInstance[] membersArray;
    int length = (membersArray = getMembersArray()).length;
    int i = 0;
    while (i < length) {
      L1PcInstance pc = membersArray[i];
      if (pc.getMapId() == 8523)
        L1Teleport.teleport(pc, 33442, 32797, (short)4, pc.getHeading(), true); 
      i++;
    } 
    clearMembers();
    clearColosseum();
  }
  
  private void clearColosseum() {
    Iterator<L1Object> iterator = World.get().getVisibleObjects(8523).values().iterator();
    while (iterator.hasNext()) {
      Object obj = iterator.next();
      if (obj instanceof L1MonsterInstance) {
        L1MonsterInstance mob = (L1MonsterInstance)obj;
        if (mob.isDead())
          continue; 
        mob.setDead(true);
        mob.setStatus(8);
        mob.setCurrentHpDirect(0);
        mob.deleteMe();
        continue;
      } 
      if (!(obj instanceof L1Inventory))
        continue; 
      L1Inventory inventory = (L1Inventory)obj;
      inventory.clearItems();
    } 
  }
  
  private void spawnBoss(int npcid, String msg1, String msg2) {
    if (msg1.equalsIgnoreCase("9")) {
      sendMessage("第 " + msg1 + " 关 [" + getBossName(npcid) + "] 30秒后开始第 " + msg2 + " 关");
    } else if (msg1.equalsIgnoreCase("10")) {
      sendMessage("最后一关 [" + getBossName(npcid) + "] 请努力撑下去，1分钟后将关闭经验副本");
    } else {
      sendMessage("第 " + msg1 + " 关 [" + getBossName(npcid) + "] 15秒后开始第 " + msg2 + " 关");
    } 
    spawn(npcid);
  }
  
  private void spawn(int npcid) {
    try {
      L1NpcInstance npc = NpcTable.get().newNpcInstance(npcid);
      npc.setId(IdFactory.get().nextId());
      npc.setMap((short)8523);
      npc.setX(32701 + RandomArrayList.getInt(7));
      npc.setY(32894 + RandomArrayList.getInt(7));
      Thread.sleep(1L);
      npc.setHomeX(npc.getX());
      npc.setHomeY(npc.getY());
      npc.setHeading(4);
      World.get().storeObject((L1Object)npc);
      World.get().addVisibleObject((L1Object)npc);
      npc.turnOnOffLight();
      npc.startChat(0);
    } catch (Exception exception) {}
  }
  
  private String getBossName(int npcId) {
    String BossName = null;
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("select name from npc where npcid = ?");
      pstm.setInt(1, npcId);
      rs = pstm.executeQuery();
      if (rs.next())
        BossName = rs.getString("name"); 
    } catch (SQLException sQLException) {
    
    } finally {
      SQLUtil.close(rs, pstm, con);
    } 
    return BossName;
  }
  
  private void sendMessage(String msg) {
    L1PcInstance[] membersArray;
    int length = (membersArray = getMembersArray()).length;
    int i = 0;
    while (i < length) {
      L1PcInstance pc = membersArray[i];
      if (pc.getMapId() == 8523)
        pc.sendPackets((ServerBasePacket)new S_BlueMessage(166, "\\f3" + msg)); 
      i++;
    } 
  }
  
  private void setTextTaskStatus(int i) {
    this._TextTaskStatus = i;
  }
  
  private int getTextTaskStatus() {
    return this._TextTaskStatus;
  }
  
  private void clearMembers() {
    this._members.clear();
  }
  
  private boolean isMember(L1PcInstance pc) {
    return this._members.contains(pc);
  }
  
  private L1PcInstance[] getMembersArray() {
    return this._members.<L1PcInstance>toArray(new L1PcInstance[this._members.size()]);
  }
  
  private int getMembersCount() {
    return this._members.size();
  }
  
  private void spawn(int npcId, int x, int y, int heading, short mapid) {
    try {
      L1NpcInstance npc = NpcTable.get().newNpcInstance(npcId);
      npc.setId(IdFactoryNpc.get().nextId());
      npc.setMap(mapid);
      npc.setX(x);
      npc.setY(y);
      npc.setHeading(heading);
      World.get().storeObject((L1Object)npc);
      World.get().addVisibleObject((L1Object)npc);
      npc.turnOnOffLight();
      npc.startChat(0);
    } catch (Exception exception) {}
  }
  
  private void setDailyGameTime07(int i) {
    this._DailyGameTime07 = i;
  }
  
  public int getDailyGameTime07() {
    return this._DailyGameTime07;
  }
  
  private class runTextTask implements Runnable {
    private runTextTask() {}
    
    public void run() {
      try {
        exp_game07.this.sendMessage("经验副本倒数5秒后开始!!");
        exp_game07.this.setTextTaskStatus(2);
        GeneralThreadPool.get().execute(new exp_game07.TimeUpdater07());
        exp_game07.this.sendMessage("经验副本启动-5秒");
        Thread.sleep(1000L);
        exp_game07.this.sendMessage("经验副本启动-4秒");
        Thread.sleep(1000L);
        exp_game07.this.sendMessage("经验副本启动-3秒");
        Thread.sleep(1000L);
        exp_game07.this.sendMessage("经验副本启动-2秒");
        Thread.sleep(1000L);
        exp_game07.this.sendMessage("经验副本启动-1秒");
        Thread.sleep(1000L);
        exp_game07.this.sendMessage("***经验副本已启动***");
        exp_game07.this.setTextTaskStatus(2);
        Thread.sleep(2000L);
        short mapId = 8523;
        int x1 = 32759;
        int y1 = 32765;
        int x2 = 32764;
        int y2 = 32765;
        int x3 = 32771;
        int y3 = 32765;
        int x4 = 32777;
        int y4 = 32765;
        int x5 = 32779;
        int y5 = 32773;
        int x6 = 32772;
        int y6 = 32773;
        int x7 = 32764;
        int y7 = 32773;
        int x8 = 32757;
        int y8 = 32773;
        int x9 = 32758;
        int y9 = 32781;
        int x10 = 32764;
        int y10 = 32781;
        int x11 = 32772;
        int y11 = 32781;
        int x12 = 32778;
        int y12 = 32781;
        exp_game07.this.spawn(81931, 32759, 32765, 4, (short)8523);
        exp_game07.this.spawn(81932, 32764, 32765, 4, (short)8523);
        exp_game07.this.spawn(81933, 32771, 32765, 4, (short)8523);
        exp_game07.this.spawn(81934, 32777, 32765, 4, (short)8523);
        exp_game07.this.spawn(81935, 32779, 32773, 4, (short)8523);
        exp_game07.this.spawn(81936, 32772, 32773, 4, (short)8523);
        exp_game07.this.spawn(81937, 32764, 32773, 4, (short)8523);
        exp_game07.this.spawn(81936, 32757, 32773, 4, (short)8523);
        exp_game07.this.spawn(81935, 32758, 32781, 4, (short)8523);
        exp_game07.this.spawn(81934, 32764, 32781, 4, (short)8523);
        exp_game07.this.spawn(81933, 32772, 32781, 4, (short)8523);
        exp_game07.this.spawn(81932, 32778, 32781, 4, (short)8523);
        exp_game07.this.sendMessage("30秒后第二波怪物将会出现");
        Thread.sleep(30000L);
        exp_game07.this.spawn(81939, 32759, 32765, 4, (short)8523);
        exp_game07.this.spawn(81940, 32764, 32765, 4, (short)8523);
        exp_game07.this.spawn(81941, 32771, 32765, 4, (short)8523);
        exp_game07.this.spawn(81942, 32777, 32765, 4, (short)8523);
        exp_game07.this.spawn(81943, 32779, 32773, 4, (short)8523);
        exp_game07.this.spawn(81944, 32772, 32773, 4, (short)8523);
        exp_game07.this.spawn(81945, 32764, 32773, 4, (short)8523);
        exp_game07.this.spawn(81944, 32757, 32773, 4, (short)8523);
        exp_game07.this.spawn(81943, 32758, 32781, 4, (short)8523);
        exp_game07.this.spawn(81942, 32764, 32781, 4, (short)8523);
        exp_game07.this.spawn(81941, 32772, 32781, 4, (short)8523);
        exp_game07.this.spawn(81940, 32778, 32781, 4, (short)8523);
        exp_game07.this.sendMessage("30秒后第三波怪物将会出现");
        Thread.sleep(30000L);
        exp_game07.this.spawn(81947, 32759, 32765, 4, (short)8523);
        exp_game07.this.spawn(81948, 32764, 32765, 4, (short)8523);
        exp_game07.this.spawn(81949, 32771, 32765, 4, (short)8523);
        exp_game07.this.spawn(81950, 32777, 32765, 4, (short)8523);
        exp_game07.this.spawn(81951, 32779, 32773, 4, (short)8523);
        exp_game07.this.spawn(81952, 32772, 32773, 4, (short)8523);
        exp_game07.this.spawn(81953, 32764, 32773, 4, (short)8523);
        exp_game07.this.spawn(81952, 32757, 32773, 4, (short)8523);
        exp_game07.this.spawn(81951, 32758, 32781, 4, (short)8523);
        exp_game07.this.spawn(81950, 32764, 32781, 4, (short)8523);
        exp_game07.this.spawn(81949, 32772, 32781, 4, (short)8523);
        exp_game07.this.spawn(81948, 32778, 32781, 4, (short)8523);
        exp_game07.this.sendMessage("30秒后第四波怪物将会出现");
        Thread.sleep(30000L);
        exp_game07.this.spawn(81955, 32759, 32765, 4, (short)8523);
        exp_game07.this.spawn(81956, 32764, 32765, 4, (short)8523);
        exp_game07.this.spawn(81957, 32771, 32765, 4, (short)8523);
        exp_game07.this.spawn(81958, 32777, 32765, 4, (short)8523);
        exp_game07.this.spawn(81959, 32779, 32773, 4, (short)8523);
        exp_game07.this.spawn(81960, 32772, 32773, 4, (short)8523);
        exp_game07.this.spawn(81961, 32764, 32773, 4, (short)8523);
        exp_game07.this.spawn(81960, 32757, 32773, 4, (short)8523);
        exp_game07.this.spawn(81959, 32758, 32781, 4, (short)8523);
        exp_game07.this.spawn(81958, 32764, 32781, 4, (short)8523);
        exp_game07.this.spawn(81957, 32772, 32781, 4, (short)8523);
        exp_game07.this.spawn(81956, 32778, 32781, 4, (short)8523);
        exp_game07.this.sendMessage("30秒后第五波怪物将会出现");
        Thread.sleep(15000L);
        exp_game07.this.spawn(81963, 32759, 32765, 4, (short)8523);
        exp_game07.this.spawn(81964, 32764, 32765, 4, (short)8523);
        exp_game07.this.spawn(81965, 32771, 32765, 4, (short)8523);
        exp_game07.this.spawn(81966, 32777, 32765, 4, (short)8523);
        exp_game07.this.spawn(81967, 32779, 32773, 4, (short)8523);
        exp_game07.this.spawn(81968, 32772, 32773, 4, (short)8523);
        exp_game07.this.spawn(81969, 32764, 32773, 4, (short)8523);
        exp_game07.this.spawn(81968, 32757, 32773, 4, (short)8523);
        exp_game07.this.spawn(81967, 32758, 32781, 4, (short)8523);
        exp_game07.this.spawn(81966, 32764, 32781, 4, (short)8523);
        exp_game07.this.spawn(81965, 32772, 32781, 4, (short)8523);
        exp_game07.this.spawn(81964, 32778, 32781, 4, (short)8523);
        exp_game07.this.sendMessage("30秒后第六波怪物将会出现");
        Thread.sleep(30000L);
        exp_game07.this.spawn(81971, 32759, 32765, 4, (short)8523);
        exp_game07.this.spawn(81972, 32764, 32765, 4, (short)8523);
        exp_game07.this.spawn(81973, 32771, 32765, 4, (short)8523);
        exp_game07.this.spawn(81974, 32777, 32765, 4, (short)8523);
        exp_game07.this.spawn(81975, 32779, 32773, 4, (short)8523);
        exp_game07.this.spawn(81974, 32772, 32773, 4, (short)8523);
        exp_game07.this.spawn(81973, 32764, 32773, 4, (short)8523);
        exp_game07.this.spawn(81972, 32757, 32773, 4, (short)8523);
        exp_game07.this.spawn(81971, 32758, 32781, 4, (short)8523);
        exp_game07.this.spawn(81974, 32764, 32781, 4, (short)8523);
        exp_game07.this.spawn(81973, 32772, 32781, 4, (short)8523);
        exp_game07.this.spawn(81972, 32778, 32781, 4, (short)8523);
        exp_game07.this.sendMessage("30秒后第七波怪物将会出现");
        Thread.sleep(30000L);
        exp_game07.this.spawn(81977, 32759, 32765, 4, (short)8523);
        exp_game07.this.spawn(81978, 32764, 32765, 4, (short)8523);
        exp_game07.this.spawn(81979, 32771, 32765, 4, (short)8523);
        exp_game07.this.spawn(81980, 32777, 32765, 4, (short)8523);
        exp_game07.this.spawn(81981, 32779, 32773, 4, (short)8523);
        exp_game07.this.spawn(81982, 32772, 32773, 4, (short)8523);
        exp_game07.this.spawn(81983, 32764, 32773, 4, (short)8523);
        exp_game07.this.spawn(81982, 32757, 32773, 4, (short)8523);
        exp_game07.this.spawn(81981, 32758, 32781, 4, (short)8523);
        exp_game07.this.spawn(81980, 32764, 32781, 4, (short)8523);
        exp_game07.this.spawn(81979, 32772, 32781, 4, (short)8523);
        exp_game07.this.spawn(81978, 32778, 32781, 4, (short)8523);
        exp_game07.this.sendMessage("30秒后第八波怪物将会出现");
        Thread.sleep(30000L);
        exp_game07.this.spawn(81985, 32759, 32765, 4, (short)8523);
        exp_game07.this.spawn(81986, 32764, 32765, 4, (short)8523);
        exp_game07.this.spawn(81987, 32771, 32765, 4, (short)8523);
        exp_game07.this.spawn(81988, 32777, 32765, 4, (short)8523);
        exp_game07.this.spawn(81989, 32779, 32773, 4, (short)8523);
        exp_game07.this.spawn(81990, 32772, 32773, 4, (short)8523);
        exp_game07.this.spawn(81991, 32764, 32773, 4, (short)8523);
        exp_game07.this.spawn(81992, 32757, 32773, 4, (short)8523);
        exp_game07.this.spawn(81990, 32758, 32781, 4, (short)8523);
        exp_game07.this.spawn(81989, 32764, 32781, 4, (short)8523);
        exp_game07.this.spawn(81988, 32772, 32781, 4, (short)8523);
        exp_game07.this.spawn(81987, 32778, 32781, 4, (short)8523);
        exp_game07.this.sendMessage("30秒后第九波怪物将会出现");
        Thread.sleep(30000L);
        exp_game07.this.spawn(81994, 32759, 32765, 4, (short)8523);
        exp_game07.this.spawn(81995, 32764, 32765, 4, (short)8523);
        exp_game07.this.spawn(81996, 32771, 32765, 4, (short)8523);
        exp_game07.this.spawn(81997, 32777, 32765, 4, (short)8523);
        exp_game07.this.spawn(81998, 32779, 32773, 4, (short)8523);
        exp_game07.this.spawn(81999, 32772, 32773, 4, (short)8523);
        exp_game07.this.spawn(82000, 32764, 32773, 4, (short)8523);
        exp_game07.this.spawn(81999, 32757, 32773, 4, (short)8523);
        exp_game07.this.spawn(81998, 32758, 32781, 4, (short)8523);
        exp_game07.this.spawn(81997, 32764, 32781, 4, (short)8523);
        exp_game07.this.spawn(81996, 32772, 32781, 4, (short)8523);
        exp_game07.this.spawn(81995, 32778, 32781, 4, (short)8523);
        exp_game07.this.sendMessage("60秒后第后一波怪物将会出现");
        Thread.sleep(60000L);
        exp_game07.this.spawn(82010, 32759, 32765, 4, (short)8523);
        exp_game07.this.spawn(82011, 32764, 32765, 4, (short)8523);
        exp_game07.this.spawn(82012, 32771, 32765, 4, (short)8523);
        exp_game07.this.spawn(82013, 32777, 32765, 4, (short)8523);
        exp_game07.this.spawn(82014, 32779, 32773, 4, (short)8523);
        exp_game07.this.spawn(82015, 32772, 32773, 4, (short)8523);
        exp_game07.this.spawn(82016, 32764, 32773, 4, (short)8523);
        exp_game07.this.spawn(82015, 32757, 32781, 4, (short)8523);
        exp_game07.this.spawn(82014, 32758, 32781, 4, (short)8523);
        exp_game07.this.spawn(82013, 32764, 32781, 4, (short)8523);
        exp_game07.this.spawn(82012, 32772, 32781, 4, (short)8523);
        exp_game07.this.spawn(82011, 32778, 32781, 4, (short)8523);
        exp_game07.this.sendMessage("120秒后将结束经验副本.请多留意时间");
        Thread.sleep(120000L);
        exp_game07.this.endTextTask();
        Thread.sleep(2000L);
        exp_game07.this.setTextTaskStatus(0);
      } catch (InterruptedException e) {
        e.printStackTrace();
      } 
    }
  }
  
  private class TimeUpdater07 implements Runnable {
    private TimeUpdater07() {}
    
    public void run() {
      int _remnant = 480;
      while (_remnant > 0) {
        try {
          exp_game07.this.setDailyGameTime07(_remnant);
          Thread.sleep(1000L);
          _remnant--;
        } catch (InterruptedException interruptedException) {}
      } 
    }
  }
}
